//
//  WZPSubTagCell.m
//  WZPItem
//
//  Created by 王王喆平 on 16/7/7.
//  Copyright © 2016年 UI. All rights reserved.
//

#import "WZPSubTagCell.h"
#import "WZPSubTagItem.h"
#import <UIImageView+WebCache.h>

@interface WZPSubTagCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *numLabel;

@end
@implementation WZPSubTagCell

+(instancetype)subTagCell{

    return [[[NSBundle mainBundle] loadNibNamed:@"WZPSubTagCell" owner:nil options:nil] firstObject];
    
}
-(void)setFrame:(CGRect)frame{
    frame.origin.y += 10;// 设置上边间隔
    frame.origin.x += 10;// 设置右边间隔
    frame.size.width -= 20;// 设置左边间隔
    frame.size.height -= 10;//设置分割线
    
    [super setFrame:frame];

}
-(void)setItem:(WZPSubTagItem *)item{
    _item = item;
    // 将数据展示到模型上
    // 裁剪图片
    [_iconView sd_setImageWithURL:[NSURL URLWithString:item.image_list] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]options:SDWebImageLowPriority completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        // 如果图片没有加载成功的话直接返回
        if (image == nil) return ;
        // 开启位图上下文
        // opaque:不透明度 YES:黑色 NO:透明
        // scale:比例因子(像素与点比例) 0:自动识别当前比例因子
        UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
        // 描述裁剪区域
        UIBezierPath * zierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height )];
        // 设置裁剪区域
        [zierPath addClip];
        // 画图
        [image drawAtPoint:CGPointZero];
        // 取出图片
        image = UIGraphicsGetImageFromCurrentImageContext();
        // 关闭上下文
        UIGraphicsEndImageContext();
        // 对图片再次赋值
        _iconView.image = image;
        
    }];
    
    _nameLabel.text = item.theme_name;
    //将超过万人的数据更改 万字结尾
    // 设置订阅数字
   CGFloat num = [item.sub_number floatValue];
   NSString * numStr = [NSString stringWithFormat:@"%@人订阅",item.sub_number];
   if (num > 10000) {
       num = num / 10000.0;
       numStr = [NSString stringWithFormat:@"%.1f万人订阅",num];
            // 用空白代替掉 尾数 0
        numStr = [numStr stringByReplacingOccurrencesOfString:@"0" withString:@""];
        }
    _numLabel.text = numStr;

}
- (void)awakeFromNib {
    [super awakeFromNib];
    // 将头像设为圆形
    // 1) 通过layer
//    _iconView.layer.cornerRadius = _iconView.wzp_width * 0.5;
//    _iconView.layer.masksToBounds = YES;(超出区域裁剪)
    // 2)在xid中通过runtime设定layer.cornerRadius 和layer.masksToBounds属性(少用)
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
